Programs =
Algorithms + Data Structures
by Niklaus Wirth, 1976
https://en.wikipedia.org/wiki/Algorithms_%2B_Data_Structures_%3D_Programs
程式 =
演算法 + 資料
by 尼克勞斯·維爾特, 1976
https://zh.wikipedia.org/zh-tw/%E5%B0%BC%E5%85%8B%E5%8A%B3%E6%96%AF%C2%B7%E7%BB%B4%E5%B0%94%E7%89%B9
Data Structure:
剛剛上一位講者: Ning 已經教過了,忘了要記得複習喔!
numeric1 = c(2, 3, 5)
string1 = c("aa", "bb", "cc")
boolean1 = c(TRUE, FALSE, TRUE)
data1 = data.frame(numeric1, string1, boolean1)
## number1 string1 boolean1 ## 1 2 aa TRUE ## 2 3 bb FALSE ## 3 5 cc TRUE
目的是用來處理「特定資料」,
根據演算法將多個函式或是指令組成的集合
簡單說,寫一個程式,其實和 寫一段文章,或是一篇小說差不多,
數學上叫函數,
程式語言中叫函式
1. 可以把相同「目的」指令和資料整理在一起 2. 可重複使用 3. 可方便程式的閱讀
by 民明書房 Noah
?function(){}
函式名稱 = function(引數) {
程式的內容........
}
愛護生命,建議千萬不要用中文當作變數及函式名字,現在出現中文只是用來說明
請在R 中顯示:Hello World, R
print("Hello World, R")
helloworld <- function(){
print("Hello World, R")
}
helloworld <- function(my_name){
print(paste("Hello World,", my_name))
}
執行:
helloworld("DSC2015")
## [1] "Hello World, DSC2015"
helloworld:函式名稱,
my_name:此函式所帶入的參數,
print("現在的時間和日期是:")
print(date())
## [1] "現在的時間和日期是:"
## [1] "Wed Aug 19 14:43:43 2015"
print("現在的日期是:")
print(paste(substr(date(),21,24), substr(date(),5,10), substr(date(),1,3)))
## [1] "現在的日期是:" ## [1] "2015 Aug 19 Wed"
print("現在的時間是:")
print(substr(date(),12,19))
## [1] "現在的時間是:" ## [1] "14:43:43"
now1 <- function(){
print("現在的日期是:")
print(paste(substr(date(),21,24), substr(date(),5,10), substr(date(),1,3)))
print("現在的時間是:")
print(substr(date(),12,19))
}
now_time()
## [1] "現在的日期是:" ## [1] "2015 Aug 19 Wed"
## [1] "現在的時間是:" ## [1] "14:43:43"
source("iris.r")
?list example(factor)
善用「tab」按鍵
Function 編輯撰寫屬於你的第一個R程式
判斷條件控制
- if / else
迴圈控制
- for(),
- while(),
- 除錯(找蟲)
- debug() / undebug()
程式錯誤及例外處理
try() / tryCatch()
Syntex:
if (condition_1){
#Do something here....
} else if (conditon_2){
#Do something here
} else {
#Do something here
}
Note: **else if** and **else** are optional.
[TRUE] or [T]
[FALSE] or [F]
"1" == "1"
[1] TRUE
"1" == "2"
[1] FALSE
"1" <= "1"
[1] TRUE
"1" <= "2"
[1] FALSE
"1" < "2"
[1] TRUE
"1" < "1"
[1] FALSE
"1" >= "1"
[1] TRUE
"1" >= "2"
[1] FALSE
"2" > "1"
[1] TRUE
"1" > "1"
[1] FALSE
"2" != "1"
[1] TRUE
"1" != "1"
[1] FALSE
"1" == "1" && "2" == "2"
[1] TRUE
("1" == "2") && ("2" == "2")
[1] FALSE
("1" == "1") || ("2" == "2")
[1] TRUE
("1" == "2") || ("2" == "2")
[1] TRUE
("1" == "2") || ("2" == "3")
[1] FALSE
如果小明滿7歲的話,那就可以看小小兵,如果沒有的話,那就不能看。
小明 = 6
不能看 保護級年紀規定 = 大於等於7歲,而且也要小於12歲
保護級最低年紀規定 = 大於等於7歲
保護級需要父母陪同的最高年紀規定 = 小於12歲
保護級不需要父母陪同的年紀規定 = 12歲以上
if (小明 >= 保護級最低年紀規定){
print(可以看)
}"
if (小明 < 保護級最低年紀規定){
print(就是不讓你看)
}
小明 = 7;
保護級最低年紀 = 7;
if(小明 > 保護級最低年紀) {
print("YES, 小明 可以看~~")
}
[1] 小明可以看
if(小明 < 保護級最低年紀) {
print("NO, T_T 小明 不能看")
}
[1] __________
『如果』 『年份』 『除以』 『400』 『餘數等於』 『0』
『等於』『閏年』
如果((年份 對後數取餘數 400) 等於 0){
是不是閏年 = 是
}
if ((year %% 400) == 0){
lear_year = TRUE
}
可以用or
if((year %% 4) == 0){
print("Leap year")
}
if((year %% 100) == 0){
print("NOT leap year")
}
if ((year1 %% 4)==0){
if ((year1 %% 100)==0){
print("NOT leap year")
} else {
print("Leap year")
}
}
if ((year1 %% 4)==0){
if ((year1 %% 100)==0){
if ((year1 %% 400)==0)
print("Leap year")
else
print("Not leap year")
} else {
print("Leap year")
}
} else {
print("Not leap year")
}
猜數字的小遊戲
猜幾A幾B的遊戲
猜幾A幾B的遊戲:和電腦比賽 (還在寫)
圈叉遊戲
圈叉遊戲:只有平手或電腦必勝(還在寫)
河內塔
五子旗(還在寫)
撲克21點(還在寫)
1. 開始條件
2. 次數累計
3. 停止條件
for(x in 1:10) {
print(x)
}
## [1] 1 ## [1] 2 ## [1] 3 ## [1] 4 ## [1] 5 ## [1] 6 ## [1] 7 ## [1] 8 ## [1] 9 ## [1] 10
c1 = c(1:10)
for(x in c1) {
print(x)
}
## [1] 1 ## [1] 2 ## [1] 3 ## [1] 4 ## [1] 5 ## [1] 6 ## [1] 7 ## [1] 8 ## [1] 9 ## [1] 10
while(判斷式) {}
如果判斷式不成立就停止,否則一直執行迴圈的程式
while(1){
print("I love R")
}
當進入迴圈時,如果想離開迴圈的話,這時後可以用break跳出迴圈
for(年 in 開始年:結束年){
判斷是否是閏年
如果是閏年則次數 + 1
}
yearS = 1000
# yearS = as.integer(readline("input start year:"))
yearE = 2015
# yearE = as.integer(readline("input end year:"))
year_cnt = 0
for (n in yearS:yearE){
if ((year1 %% 4)==0){
if ((year1 %% 100)==0){
if ((year1 %% 400)==0)
is_leap_year = TRUE
else
is_leap_year = FALSE
} else {
is_leap_year = TRUE
}
} else {
is_leap_year = FALSE
}
if (is_leap_year == TRUE)
year_cnt = year_cnt + 1
}
}
paste("總共有:", year_cnt," 個閏年")
## [1] "總共有: 246 個閏年"
leap_year = function(year1){
if ((year1 %% 4)==0){
if ((year1 %% 100)==0){
if ((year1 %% 400)==0)
return TRUE # 是閏年
else
return FALSE # 不是閏年
} else {
return TRUE # 是閏年
}
} else {
return FALSE # 不是閏年
}
}
yearS = 1000 # 或是可以輸入:as.integer(readline("input start year:")) 來自己輸入開始年
yearE = 2015 # 或是可以輸入:as.integer(readline("input end year:")) 來輸入結束年
year_cnt=0
for (n in yearS:yearE){
if (leap_year(n)){
year_cnt = year_cnt+1
}
}
paste("總共有:", year_cnt," 個閏年")
## [1] "總共有: 246 個閏年"
每天都在問神奇海螺
人都可能被劈腿,手一定有機會出鎚,
debug(fun)
undebug(fun)
debug(now_time)
now_time()
undebug(now_time)
data_in = list(1, 2, 4, -5, "I^love^R", 0, 10)
for(input in data_in) {
print(log(input))
}
[1] "0" [1] "0.693147180559945" [1] "1.38629436111989" [1] "NaN"
Error in log(input) : non-numeric argument to mathematical function 此外: Warning message: In log(input) : 產生了 NaNs
data_in = list(1, 2, 4, -5, "I^love^R", 0, 10)
for(input in data_in) {
try(print(log(input)))
}
## [1] 0 ## [1] 0.6931472 ## [1] 1.386294
## Warning in log(input): NaNs produced
## [1] NaN ## [1] -Inf ## [1] 2.302585
func_error = function(e){
print("this is a ERROR!!!")
}
func_warning = function(w){
print("just a wraning")
}
for(input in data_in) {
tryCatch(print(log(input)),
warning = func_warning ,
error = func_error)
}
## [1] 0 ## [1] 0.6931472 ## [1] 1.386294 ## [1] "just a wraning" ## [1] "this is a ERROR!!!" ## [1] -Inf ## [1] 2.302585
[1] 0 [1] 0.6931472 [1] 1.386294
[1] "just a wraning" [1] "this is a ERROR!!!" [1] -Inf [1] 2.302585
當然可以,but
https://www.calvin.edu/~scofield/courses/m143/materials/RcmdsFromClass.pdf
https://www.calvin.edu/~scofield/courses/m143/materials/RcmdsFromClass.pdf
num1 = c(2, 3, 5)
str1 = c("aa", "bb", "cc")
logic1 = c(TRUE, FALSE, TRUE)
df1 = data.frame(num1, str1, logic1)
# df1表示把data.frame()括號內的資料結合成為data.frame型態
## num1 str1 logic1 ## 1 2 aa TRUE ## 2 3 bb FALSE ## 3 5 cc TRUE
num1 = c(2, 3, 5)
str1 = c("aa", "bb", "cc")
logic1 = c(TRUE, FALSE, TRUE)
df2 = c(num1, str1, logic1)
# df2表示為c()括號中向量資料的結合
## [1] "2" "3" "5" "aa" "bb" "cc" "TRUE" "FALSE" "TRUE"
num1 = c(2, 3, 5)
str1 = c("aa", "bb", "cc")
logic1 = c(TRUE, FALSE, TRUE)
df3 = rbind(num1, str1, logic1)
# df3表示為rbind()括號中資料的結合
## [,1] [,2] [,3] ## num1 "2" "3" "5" ## str1 "aa" "bb" "cc" ## logic1 "TRUE" "FALSE" "TRUE"
source("iris.r")
iris_csv = read.csv("iris.csv")
View(iris_csv)
iris_tab = read.table("iris.txt")
View(iris_tab)
注意:這個需要額外的套件來支援excel格式,請先執行:library(gdata)
注意2:gdata套件的read.xls只能讀excel檔案中第一頁sheet,
library(gdata)
iris_xls = read.xls("iris.xls")
View(iris_xls)